package com.xiaojie.hystrix.fallback;


import com.xiaojie.hystrix.feign.MyFeign;
import com.xiaojie.hystrix.decoder.MyFeignException;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author wangye
 * @classname MyFeignFactory
 * @date 2021/6/21 10:23
 **/
@Component
@Slf4j
public class MyFeignFallbackFactory implements FallbackFactory<MyFeign> {


    @Override
    public MyFeign create(Throwable cause) {

        return new MyFeign() {
            @Override
            public String test() {
                return getErrorMsg();
            }

            @Override
            public String test2() {
                return getErrorMsg();
            }

            @Override
            public String test3() {
                return getErrorMsg();
            }

            @Override
            public String test4() {
                return getErrorMsg();
            }

            @Override
            public String test5() {
                return getErrorMsg();
            }

            private String getErrorMsg() {
                log.error("feign error", cause);
                if (cause instanceof MyFeignException) {
                    MyFeignException feignException = (MyFeignException) cause;
                    return feignException.getMessage();
                } else {
                    return cause.getMessage();
                }
            }


        };
    }
}
